<?php
namespace App\Services\V1;

use App\Http\Controllers\API\FormatController;
use App\Models\Shops;
use App\Services\BaseService;//服务
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class ShopsServer extends BaseService{


    private object $User;
    private Request $Request;
    private array $Header;
    private string $Lang;
    private int $Limit;
    private int $Page;

    public function __construct(FormatController $forMat)
    {
       $UserData = $forMat->getAdmin();
       if($UserData['status'] === false){
           Message(__('fail.user_not_power'));
       }
       $this->User = $UserData['user'] ?? (object)[];
       $this->Request = $forMat->getRequest();
       $this->Header = $forMat->getHeader();
       $this->Lang = $forMat->getLang();
       $this->Limit = $forMat->getLimit();
       $this->Page = $forMat->getPage();
    }

    /**
     *获取模型
     * @return Builder
     */
    private function Model(): Builder
    {
        return Shops::query()->where('admin_id', $this->User->id);
    }



    public function list():array{//列表
        return [
            'shops'=>self::Model()->get(),
            'boss'=> $this->User,
        ];
    }

    public function add():bool{//添加
        Message(__("fail.api_Stop"));
        return true;
    }

    public function edit():bool{//编辑
        $input = $this->Request->validate([
            'name'=>"required|string",//店铺名称
            'description'=>"string",//店铺描述
            'logo'=>"required|string",//店铺Logo的图片路径或URL
            'address'=>"required|string",//店铺详细地址
            'phone'=>"required|string|max:11",//联系电话
            'opening_hours'=>"required|string",//营业时间
        ],[
            'name'=>__('validate.shops.name').__("validate.error"),
            'description'=>__('validate.shops.description').__("validate.error"),
            'logo'=>__('validate.shops.logo').__("validate.error"),
            'address'=>__('validate.shops.address').__("validate.error"),
            'phone'=>__('validate.shops.phone').__("validate.error"),
            'opening_hours'=>__('validate.shops.opening_hours').__("validate.error"),
        ]);
        $oldLogo = $this->Model()->first()->logo;
        $this->setFileActive($input['logo'],$oldLogo);
        $edit = $this->Model()->update($input);
        if(!$edit) Message(__('fail.edit_shops'));
        return true;
    }

    public function del():bool{//删除

        Message(__("fail.api_Stop"));
        return true;
    }

    public function details():void{//详情

    }
}
